package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.pojo.vo.CallCenterOrgGetByCorpIdVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.saas.domain.dao.CallCenterOrganizationDao;
import com.xbongbong.saas.domain.entity.CallCenterOrganizationEntity;
import com.xbongbong.saas.model.CallCenterOrganizationModel;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author youli.chen
 * @version v1.0
 * @description: 呼叫中心组织实现类
 * @date 2019/2/13 17:29
 * @since v1.0
 */
@Service("callCenterOrganizationModel")
public class CallCenterOrganizationModelImpl implements CallCenterOrganizationModel {

    @Resource
    private CallCenterOrganizationDao callCenterOrganizationDao;

    /**
     * 根据主键和公司id进行删除
     *
     * @param key    主键
     * @param corpid 公司id
     * @return Integer MyBatis操作结果 1：成功
     */
    @Override
    public Integer deleteByKey(Long key, String corpid) {
        return callCenterOrganizationDao.deleteByKey(key, corpid);
    }

    /**
     * 根据主键和公司id进行精准查找
     *
     * @param key    主键
     * @param corpid 公司id
     * @return CallCenterOrganizationEntity 话务中心企业信息
     */
    @Override
    public CallCenterOrganizationEntity getByKey(Long key, String corpid) {
        return callCenterOrganizationDao.getByKey(key, corpid);
    }

    /**
     * 获取开启了呼叫中心的公司id队列
     *
     * @param params 封装的条件
     * @return List<String> 公司id队列
     * @author youli.chen
     * @version v1.0
     * @since v1.0
     */
    @Override
    public List<String> findCorps(Map<String, Object> params) {
        return callCenterOrganizationDao.findCorps(params);
    }

    /**
     * 获取开启了呼叫中心的公司总数
     *
     * @return Integer 总数
     * @author youli.chen
     * @version v1.0
     * @since v1.0
     */
    @Override
    public Integer getCorpsCount() {
        return callCenterOrganizationDao.getCorpsCount();
    }

    /**
     * 更新企业的预充值余额
     *
     * @param corpid     公司ID
     * @param moduleType 话务中心平台  参考CallCenterTypeEnum
     * @param balance    预充值余额
     */
    @Override
    public void updateBalance(String corpid, Integer moduleType, BigDecimal balance) {
        callCenterOrganizationDao.updateBalance(corpid, moduleType, balance);
    }

    /**
     * 插入
     *
     * @param entity 实体
     * @return 是否成功
     * @throws XbbException 数据库操作异常
     */
    @Override
    public Integer insert(CallCenterOrganizationEntity entity) throws XbbException {
        return callCenterOrganizationDao.insert(entity);
    }

    @Override
    public Integer update(CallCenterOrganizationEntity entity) throws XbbException {
        return callCenterOrganizationDao.update(entity);
    }

    @Override
    public CallCenterOrgGetByCorpIdVO getByCorpId(String corpid, Integer moduleType) throws XbbException {
        Map<String, Object> params = new HashMap<>(6);
        params.put("corpid", corpid);
        params.put("moduleType", moduleType);
        params.put("del", DelEnum.NORMAL.getDel());
        params.put("start", 0);
        params.put("pageNum", 1);
        List<CallCenterOrganizationEntity> list = findEntitys(params);
        CallCenterOrgGetByCorpIdVO getByCorpIdVO = new CallCenterOrgGetByCorpIdVO();
        if (list.size() != 0) {
            BeanUtils.copyProperties(list.get(0), getByCorpIdVO);
            getByCorpIdVO.setApikey(list.get(0).getApikey());
            getByCorpIdVO.setStartTime(list.get(0).getStarTime());
        }
        return getByCorpIdVO;
    }

    /**
     * 获取符合查询条件的记录
     *
     * @param param 查询条件
     * @return 符合查询条件的记录
     */
    @Override
    public List<CallCenterOrganizationEntity> findEntitys(Map<String, Object> param) {
        return callCenterOrganizationDao.findEntitys(param);
    }

    /**
     * 获取符合查询条件的记录总数
     *
     * @param param 查询条件
     * @return 符合查询条件的记录总数
     */
    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return callCenterOrganizationDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }
}
